
<?php

class Product{
private $storeID;
private $size;
private $quantity;
private $proCatagory ;
private $proDesc ;
private $proPrice ;
private $proId ;
private $image ;
private $db;
////////////////////////////category
function __construct($storeID=null,$category=null,$size=null,$image=false,$price=null,$quantity=null,$proDesc=null){
      //set data
      
      $this->setData($storeID,$category,$size,$image,$price,$quantity,$proDesc);
        //connect db
      
       $this->connectDB();
       
     //insert to database
      if($storeID==null) {
         // die("ERROR: you have to create store first.");
      }
       else{
              if($this->image==NULL)
              {
                  die("sorry you cant't add product without uploading an image.");
              }
           $this->addProduct();
           
       }
    
}
private function setData($storeID,$category,$size,$image,$price,$quantity,$proDesc){
    $this->storeID=$storeID;
    $this->proPrice=$price;
    $this->quantity=$quantity;
    $this->size=$size;
    $this->proCatagory=$category;
    $this->proDesc=$proDesc;
    if($image!=false){
        include 'imageClass.php';
        $img = new imgUploader();
        $this->image=  $img->UploadPath($image);
    }
 else {
        $this->image=null;
    }
}
private function connectDB(){
        
      try {
          include'database.php';
     $db_config_file="db_config.php";
     $this->db=new database($db_config_file);
}   
       catch (Exception $ex) {
       $ex->getMessage(); 
}
  
    }
   
///////////////////////////////
///////////////////////////// return 



/////////////////////////// return
function getProductByID($id){
$sql="select * from product where proID='$id'";
$result=mysql_query($sql);

if($result){
$row[0] = mysql_fetch_array($result);
  if(count($row)>0)
     {
     
      return $row;
     }
  }
  else{
      die("ERROR:can't get this product.");
    }
}
///////////////////////////////
function getStoreProduct($storeID){
$sql="select * from product where stID='$storeID'";
if(!$result=mysql_query($sql))
    {
        die("Error:can't get product from database.");
    
    }
    else{
        $num=  mysql_num_rows($result);
        if($num > 0){
            
            for($i=0;$i<$num;$i++)
            {
               $row[$i] = mysql_fetch_array($result); 
            } 
             return $row;
        }
        
    }
    
   
    
}
//////////////////////////////////////
function getProducts(){
$sql="select * from product";
if(!$result=mysql_query($sql))
    {
       die("Error:can't get products from database.");
    
    }
    else{
        $num=  mysql_num_rows($result);
        if($num > 0){
            
            for($i=0;$i<$num;$i++)
            {
               $row[$i] = mysql_fetch_array($result); 
            }  
             return $row;
        }
        
    }
    
   
    
}
////////////////////////////////////////

function addProduct(){

$sql="INSERT INTO product (stID,category,size,proImage,price,quantity,proDesc)
VALUES ('$this->storeID','$this->proCatagory','$this->size','$this->image','$this->proPrice','$this->quantity','$this->proDesc')";

if (!mysql_query($sql))
{
  die('Error:failed to ADD product try another time');
}
else {
    
   return true;

   
}
}
///////////////////////

public function UpdateProduct($proID,$category=null,$size=null,$image=false,$price=null,$quantity=null,$proDesc=null){
        if($image!=false){
        include 'imageClass.php';
        $img = new imgUploader();
        $image=  $img->UploadPath($image);
    }
 else {
        $image=null;
        //throw new Exception("sorry you cant't edit product without uploading an image.");
         $sql="UPDATE product
           set category='$category',size='$size',price='$price',quantity='$$quantity',proDesc='$proDesc'"
               . "where proID='$proID'";

if (!mysql_query($sql))
{
  die('Error:failed to ADD product try another time');
}
else {
    
   return true;

   
}
    }
       $sql="UPDATE product
           set category='$category',size='$size',proImage='$image',price='$price',quantity='$$quantity',proDesc='$proDesc'"
               . "where proID='$proID'";

if (!mysql_query($sql))
{
  die('Error:failed to ADD product try another time');
}
else {
    
   return true;

   
} 
        
    }
//////////////////////
function displayProductsByCategory($storeID,$cat){
     $sql="select * from product where stID='$storeID' AND category='$cat'";
    if(!$result=mysql_query($sql))
    {
        die("Error:can't get products from database.");
    
    }
    else{
        $num=  mysql_num_rows($result);
        if($num > 0){
            
            for($i=0;$i<$num;$i++)
            {
               $row[$i] = mysql_fetch_array($result);
              // $this->displayProduct($row);
             // echo $row[$i]['proID'];
            }  
             return $row;
        }
       
    }
}
    
   // return $row;

 
	/// $ImagePath=uploadPath($Image_Name ,$Image_temp_name);
//if($ImagePath !=false){
	 //addProduct($storeID,$category,$size,$ImagePath,$price,$quantity);
	// header('Location:products.html');
//}
	
}

?>

